package org.huel.backend.mapper;


import org.apache.ibatis.annotations.*;
import org.huel.backend.domain.dto.StudentRecommendationDTO;
import org.huel.backend.domain.entity.UserRecommendation;

import java.util.List;

@Mapper
public interface UserRecommendationMapper {
    /**
     * 根据学生ID获取推荐课程（按推荐分数排序）
     * @param studentId 学生ID
     * @param limit 限制数量
     * @return 推荐列表
     */
    @Select("SELECT * FROM user_recommendations WHERE student_id = #{studentId} " +
            "ORDER BY recommendation_score DESC, rank_order ASC LIMIT #{limit}")
    @Results({
            @Result(property = "studentId", column = "student_id"),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "recommendationScore", column = "recommendation_score"),
            @Result(property = "recommendationType", column = "recommendation_type"),
            @Result(property = "rankOrder", column = "rank_order"),
            @Result(property = "lastUpdated", column = "last_updated")
    })
    List<UserRecommendation> getRecommendationsByStudentId(@Param("studentId") String studentId, @Param("limit") int limit);

    /**
     * 根据推荐类型获取推荐结果
     * @param recommendationType 推荐类型
     * @param limit 限制数量
     * @return 推荐列表
     */
    @Select("SELECT * FROM user_recommendations WHERE recommendation_type = #{recommendationType} " +
            "ORDER BY recommendation_score DESC, rank_order ASC LIMIT #{limit}")
    @Results({
            @Result(property = "studentId", column = "student_id"),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "recommendationScore", column = "recommendation_score"),
            @Result(property = "recommendationType", column = "recommendation_type"),
            @Result(property = "rankOrder", column = "rank_order"),
            @Result(property = "lastUpdated", column = "last_updated")
    })
    List<UserRecommendation> getRecommendationsByType(@Param("recommendationType") String recommendationType, @Param("limit") int limit);

    /**
     * 获取所有学生ID列表
     * @return 学生ID列表
     */
    @Select("SELECT DISTINCT student_id FROM user_recommendations ORDER BY student_id")
    List<String> getAllStudentIds();

    /**
     * 获取所有推荐类型
     * @return 推荐类型列表
     */
    @Select("SELECT DISTINCT recommendation_type FROM user_recommendations ORDER BY recommendation_type")
    List<String> getAllRecommendationTypes();

    /**
     * 获取热门推荐课程（综合所有学生的推荐分数）
     * @param limit 限制数量
     * @return 热门课程列表
     */
    @Select("SELECT course_name, AVG(recommendation_score) as avg_score, COUNT(*) as recommend_count " +
            "FROM user_recommendations GROUP BY course_name " +
            "ORDER BY avg_score DESC, recommend_count DESC LIMIT #{limit}")
    @Results({
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "recommendationScore", column = "avg_score"),
            @Result(property = "rankOrder", column = "recommend_count")
    })
    List<UserRecommendation> getPopularCourses(@Param("limit") int limit);

    /**
     * 插入或更新推荐数据
     * @param recommendation 推荐对象
     */
    @Insert("INSERT INTO user_recommendations (student_id, course_name, recommendation_score, recommendation_type, rank_order, last_updated) " +
            "VALUES (#{studentId}, #{courseName}, #{recommendationScore}, #{recommendationType}, #{rankOrder}, NOW()) " +
            "ON DUPLICATE KEY UPDATE " +
            "recommendation_score = VALUES(recommendation_score), " +
            "rank_order = VALUES(rank_order), " +
            "last_updated = NOW()")
    void insertOrUpdate(UserRecommendation recommendation);
}